﻿@page "/items"
@using Wms.Core.Entities
@using Microsoft.EntityFrameworkCore
@inject Wms.Data.WarehouseDbContext Db

<div class="container-fluid py-4">
    <h4 class="text-primary mb-4">
        <span class="oi oi-tag me-2"></span> 物料管理
    </h4>

    <!-- 添加物品表单 -->
    <div class="card shadow-sm mb-4">
        <div class="card-body">
            <div class="row g-3">
                <div class="col-md-4">
                    <input type="text"
                           class="form-control form-control-lg"
                           @bind="newItemName"
                           placeholder="请输入物品名称" />
                </div>
                <div class="col-md-3">
                    <input type="text"
                           class="form-control form-control-lg"
                           @bind="newItemCode"
                           placeholder="物品编码" />
                </div>
                <div class="col-md-3">
                    <input type="text"
                           class="form-control form-control-lg"
                           @bind="newItemSpec"
                           placeholder="规格（如：500g/瓶）" />
                </div>
                <div class="col-md-2 d-grid">
                    <button @onclick="AddItem" class="btn btn-success btn-lg">
                        <span class="oi oi-plus me-1"></span> 添加
                    </button>
                </div>
            </div>
        </div>
    </div>

    <!-- 物品列表 -->
    <div class="card shadow-sm">
        <div class="card-header bg-white d-flex justify-content-between align-items-center">
            <h6 class="mb-0">物料列表</h6>
            <span class="badge bg-info text-dark rounded-pill">@items.Count</span>
        </div>
        <div class="card-body p-0">
            @if (!items.Any())
            {
                <div class="text-center py-5">
                    <img src="https://cdn.jsdelivr.net/gh/twbs/icons/icons/box.svg" alt="空" width="48" class="mb-3 opacity-50" />
                    <p class="text-muted">暂无物料，请先添加。</p>
                </div>
            }
            else
            {
            <div class="table-responsive">
                <table class="table table-hover align-middle mb-0">
                    <thead class="table-light">
                        <tr>
                            <th scope="col" style="width: 80px;">编号</th>
                            <th scope="col">名称</th>
                            <th scope="col" style="width: 150px;">编码</th>
                            <th scope="col" style="width: 150px;">规格</th>
                            <th scope="col" style="width: 150px;">操作</th>
                        </tr>
                    </thead>
                    <tbody>
                            @if (items.Any())
                            {
                                @foreach (var item in items)
                                {
                                    <tr>
                                        <td><strong>@item.Id</strong></td>
                                        <td>@item.Name</td>
                                        <td><code>@item.Code</code></td>
                                        <td class="text-muted">@item.Spec</td>
                                        <td>
                                            <button @onclick="() => EditItem(item)"
                                                    class="btn btn-sm btn-outline-primary me-2"
                                                    title="编辑">
                                                <span class="oi oi-pencil"></span>
                                            </button>
                                            <button @onclick="() => DeleteItem(item)"
                                                    class="btn btn-sm btn-outline-danger"
                                                    title="删除">
                                                <span class="oi oi-trash"></span>
                                            </button>
                                        </td>
                                    </tr>
                                }
                            }
                            else
                            {
                                <tr>
                                    <td colspan="5" class="text-center text-secondary py-4">
                                        <em>暂无物品数据</em>
                                    </td>
                                </tr>
                            }
                    </tbody>
                </table>
            </div>
        }
        </div>
    </div>
</div>

<!-- 删除确认模态框（可选增强） -->
@if (showDeleteConfirm)
{
    <div class="modal fade show d-block" style="background-color: rgba(0,0,0,0.5);" tabindex="-1" role="dialog">
        <div class="modal-dialog" role="document">
            <div class="modal-content">
                <div class="modal-header">
                    <h5 class="modal-title text-danger">确认删除？</h5>
                </div>
                <div class="modal-body">
                    确定要删除物品 "<strong>@itemToDelete?.Name</strong>" 吗？此操作不可撤销。
                </div>
                <div class="modal-footer">
                    <button @onclick="() => showDeleteConfirm = false" class="btn btn-secondary">取消</button>
                    <button @onclick="ConfirmDelete" class="btn btn-danger">确认删除</button>
                </div>
            </div>
        </div>
    </div>
}
@if (showEditModal)
{
    <div class="modal fade show d-block" style="background-color: rgba(0,0,0,0.5);">
        <div class="modal-dialog">
            <div class="modal-content">
                <div class="modal-header">
                    <h5 class="modal-title">编辑物品</h5>
                </div>
                <div class="modal-body">
                    <input class="form-control mb-2" @bind="editItemName" placeholder="物品名称" />
                    <input class="form-control mb-2" @bind="editItemCode" placeholder="物品编码" />
                    <input class="form-control" @bind="editItemSpec" placeholder="规格" />
                </div>
                <div class="modal-footer">
                    <button class="btn btn-secondary" @onclick="() => showEditModal = false">取消</button>
                    <button class="btn btn-primary" @onclick="SaveEdit">保存</button>
                </div>
            </div>
        </div>
    </div>
}
@code {
    private List<Wms.Core.Entities.Item> items = new();
    private string newItemName = "";
    private string newItemCode = "";
    private string newItemSpec = "";

    private bool showEditModal = false;
    private Wms.Core.Entities.Item itemToEdit = new();
    private string editItemName;
    private string editItemCode;
    private string editItemSpec;
    
    // 删除确认相关
    private bool showDeleteConfirm = false;
    private Wms.Core.Entities.Item itemToDelete;

    protected override async Task OnInitializedAsync()
    {
        await LoadItems();
    }

    private async Task LoadItems()
    {
        items = await Db.Items.ToListAsync();
    }

    private async Task AddItem()
    {
        if (string.IsNullOrWhiteSpace(newItemName))
        {
            return;
        }

        var item = new Wms.Core.Entities.Item
            {
                Name = newItemName.Trim(),
                Code = newItemCode?.Trim(),
                Spec = newItemSpec?.Trim()
            };

        Db.Items.Add(item);
        await Db.SaveChangesAsync();

        await LoadItems(); // 刷新

        // 清空输入框
        newItemName = "";
        newItemCode = "";
        newItemSpec = "";
    }

    private void EditItem(Wms.Core.Entities.Item item)
    {
        itemToEdit = item;
        editItemName = item.Name;
        editItemCode = item.Code;
        editItemSpec = item.Spec;
        showEditModal = true;
    }
    private async Task SaveEdit()
    {
        if (itemToEdit != null)
        {
            itemToEdit.Name = editItemName;
            itemToEdit.Code = editItemCode;
            itemToEdit.Spec = editItemSpec;

            await Db.SaveChangesAsync();
            await LoadItems();
        }

        showEditModal = false;
        itemToEdit = null;
    }
    private void DeleteItem(Wms.Core.Entities.Item item)
    {
        itemToDelete = item;
        showDeleteConfirm = true;
    }

    private async Task ConfirmDelete()
    {
        if (itemToDelete != null)
        {
            Db.Items.Remove(itemToDelete);
            await Db.SaveChangesAsync();
            await LoadItems();
        }
        showDeleteConfirm = false;
        itemToDelete = null;
    }
}